import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isShowTip: false,
    info: [],
    originText: '',
    index: 0,
    showTimeFlag: false,
    searchInfo: [],
    searchContent: ''
  },
  mutations: {
    changeShowTip (state, flag) {
      state.isShowTip = flag
    },

    getInfo (state, content, flag) {
      if (!flag) return state.info.push(content)
      if (content.text === '') state.info.splice(state.index, 1)
    },

    changeShowTime (state, index) {
      if (state.showTimeFlag) {
        state.showTimeFlag = false
        return
      }
      if (state.searchContent === '') {
        state.info[index].showTime = !state.info[index].showTime
      } else {
        state.searchInfo[index].showTime = !state.searchInfo[index].showTime
      }
    },

    delTip (state, index) {
      if (state.searchContent === '') {
        state.info.splice(index, 1)
        state.info.forEach(item => {
          item.showTime = false
        })
      } else {
        const text = state.searchInfo[index].text
        state.info = state.info.filter(item => {
          return item.text !== text
        })
        state.searchInfo.splice(index, 1)
        state.searchInfo.forEach(item => {
          item.showTime = false
        })
      }

      state.showTimeFlag = true
    },

    showOriginText (state, index) {
      if (state.searchContent === '') {
        state.originText = state.info[index].text
      } else {
        state.originText = state.searchInfo[index].text
      }
      state.index = index
    },

    changeTipContent (state, content) {
      if (state.searchContent === '') {
        if (content !== '') {
          state.info[state.index].text = content
          return
        }
        state.info.splice(state.index, 1)
      }
    },

    searchTips (state, content) {
      state.searchContent = content
      if (content === '') {
        state.searchInfo = []
        return
      }
      const list = state.info.filter(item => {
        if (item.text.includes(content)) return item
      })
      state.searchInfo = list
    }
  }
})
